package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.adapter.CarfromTwoAdapter;
import com.bjesc_b2c.app.appraiset.base.BaseActivity;
import com.bjesc_b2c.app.appraiset.mvp.presenter.GetCarForPresenter;
import com.bjesc_b2c.app.appraiset.mvp.presenter.impl.GetCarForPresenterImpl;
import com.bjesc_b2c.app.appraiset.mvp.view.CarForView;
import com.bjesc_b2c.app.appraiset.widget.view.IndexView;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.MultiItemTypeAdapter;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import master_bean.CarForBean;

/**
 * Created by Jason Chen on 2017/2/22.
 * 车辆来源
 */

public class CarFromActivity extends BaseActivity implements CarForView, IndexView.OnSetIndexListenre {
    @InjectView(R.id.recycler_view)
    RecyclerView carfromTwoRecycler;
    @InjectView(R.id.index_view)
    IndexView indexView;
    private CarfromTwoAdapter twoAdapter;
    private List<CarForBean> carForBean;
    private String carFofId;
    private Intent intent;
    private String twoName;
    private LinearLayoutManager twoManager;
    private GetCarForPresenter carForPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_from);
        ButterKnife.inject(this);
        intent = new Intent();
        getList();
    }

    private void initViews() {
        indexView.setVisibility(View.VISIBLE);
        twoAdapter = new CarfromTwoAdapter(this, R.layout.layout_item_select_car_brand, carForBean, CarfromTwoAdapter.TYPE_ONE);
        twoManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        carfromTwoRecycler.setLayoutManager(twoManager);
        carfromTwoRecycler.setAdapter(twoAdapter);
        twoAdapter.setOnItemClickListener(twoListener);
        carfromTwoRecycler.setOnScrollListener(scrollListener);
        indexView.setOnSetIndexListenre(this);
    }

    private RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            int position = twoManager.findFirstVisibleItemPosition();
            indexView.refresh(carForBean.get(position).getFirstLetter().charAt(0));
        }
    };

    private MultiItemTypeAdapter.OnItemClickListener twoListener = new MultiItemTypeAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
            twoName = carForBean.get(position).getName();
            carFofId = carForBean.get(position).getId();
            intent.putExtra("carForName", twoName);
            intent.putExtra("carFofId", "29," + carFofId);
            setResult(3, intent);
            finish();
        }

        @Override
        public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
            return false;
        }
    };

    @OnClick(R.id.iv_return)
    public void onClick() {
        finish();
    }

    public void getList() {
//        if (getIntent().getSerializableExtra("carForBean") == null) {
//            carForBean = TimeListUilts.getCarForBean(this);
//        } else {
//            carForBean = (List<CarForBean>) getIntent().getSerializableExtra("carForBean");
//        }

        if (carForBean == null) {
            String marketID = getIntent().getStringExtra("marketId");
            carForPresenter = new GetCarForPresenterImpl(CarFromActivity.this, this);
            carForPresenter.getCarFor(marketID);
        } else {
            List<CarForBean> list = (List<CarForBean>) getIntent().getSerializableExtra("CarforList");
            if (list.size() > 0) {
                Collections.sort(list, new Comparator<CarForBean>() {
                    public int compare(CarForBean carBrandBean1, CarForBean carBrandBean2) {
                        if (carBrandBean1.getFirstLetter().charAt(0) > carBrandBean2.getFirstLetter().charAt(0)) {
                            return 1;
                        }
                        if (carBrandBean1.getFirstLetter().charAt(0) == carBrandBean2.getFirstLetter().charAt(0)) {
                            return 0;
                        }
                        return -1;
                    }
                });
                carForBean = list;
                initViews();
                twoAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void getCarForDealer(List<CarForBean> listbean) {

        Collections.sort(listbean, new Comparator<CarForBean>() {
            public int compare(CarForBean carBrandBean1, CarForBean carBrandBean2) {
                if (carBrandBean1.getFirstLetter().charAt(0) > carBrandBean2.getFirstLetter().charAt(0)) {
                    return 1;
                }
                if (carBrandBean1.getFirstLetter().charAt(0) == carBrandBean2.getFirstLetter().charAt(0)) {
                    return 0;
                }
                return -1;
            }
        });
        carForBean = listbean;
        initViews();
        twoAdapter.notifyDataSetChanged();
    }

    @Override
    public void setIndexListenre(char index) {
        for (int i = 0; i < carForBean.size(); i++) {
            if (String.valueOf(index).equals(carForBean.get(i).getFirstLetter())) {
                twoManager.scrollToPositionWithOffset(i, 0);
                break;//加了break显示才会更准确—yd
            }
        }
    }
}
